/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package educar.abmCursa;

import educar.models.Alumno;
import educar.models.Cursa;
import educar.models.Materia;
import educar.models.cursaNotFound;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Matias
 */
public class AgregarCursaController implements ActionListener {
       
    private AgregarCursarView agregarCursaView;
    private String vector[] = new String[2];
    private CursaController cursaControl;
    
// <editor-fold defaultstate="collapsed" desc="Constructor">
    public AgregarCursaController(CursaController cursaControler){        
        cursaControl = cursaControler;
        agregarCursaView = new AgregarCursarView();
        agregarCursaView.setActionListener(this);
    }
 // </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Action setView">    
    public void setView() {
        agregarCursaView.setVisible(true);
    }
// </editor-fold>  

// <editor-fold defaultstate="collapsed" desc="Metodos ActionPermormed">    
    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (agregarCursaView.getJbCancelar() == source){
            agregarCursaView.dispose();
        }
        if (agregarCursaView.getJbAceptar() == source){
            vector[0] = agregarCursaView.getCodMateria();
            vector[1] = agregarCursaView.getnroAlumno();
             if (!(vector[0].length() == 0) || (vector[1].length() == 0)) {
                if ((Materia.exist(vector[0])) && (Alumno.exist(vector[1]))) {
                    Cursa cursa = new Cursa(vector[0], vector[1]);
                    try {
                        if (cursa.save()) {                           
                            cursaControl.agregarATabla(vector);
                            agregarCursaView.dispose();
                        } else {
                            JOptionPane.showMessageDialog(null,"Ya existe un registro con el CodigodeMateria "+ vector[0]+" , y el numero de alumno"+ vector[1]+".","proyecto 2012",JOptionPane.WARNING_MESSAGE);
                        }
                    } catch (cursaNotFound ex) {
                        Logger.getLogger(AgregarCursaController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else{
                    JOptionPane.showMessageDialog(null,"Alguno de los campos no existe","proyecto 2012",JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null,"Existe al menos un campo sin completar.\n\nRevise los campos ingresados","proyecto 2012",JOptionPane.WARNING_MESSAGE);
            }
        }
    }    
// </editor-fold>

}
